package com.wzy.webviewsnapshot;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Picture;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "SnapShotActivity";
    private Bitmap mBmp = null;
    private WebView mWebView = null;
    private ImageView mImageView = null;
    private FileService mFileService = null;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout linearLayout = (LinearLayout) LinearLayout.inflate(this, R.layout.activity_main, null);
        setContentView(linearLayout);
        // 初始化view
        Button button1 = (Button) findViewById(R.id.btn01);
        Button button2 = (Button) findViewById(R.id.btn02);
        Button button3 = (Button) findViewById(R.id.btn03);
        mImageView = (ImageView) findViewById(R.id.imageView);

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.setWebViewClient(new WebViewClient(){

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                view.loadUrl(url);
                return true;
            }
        });
        mWebView.loadUrl("http://www.baidu.com");
        mWebView.setDrawingCacheEnabled(true);

        // mWebView.setVisibility(View.INVISIBLE);
        mFileService = new FileService(this);

        // 获取webView全部内容快照
        button1.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {

                mBmp = captureWebView(mWebView);

                System.out.println("bmpWidth=" + mBmp.getWidth());
                System.out.println("bmpHeight=" + mBmp.getHeight());

                mImageView.setBackgroundDrawable(new BitmapDrawable(mBmp));

                Log.i(TAG, "获取快照");

                String fileName = mFileService.saveBitmapToSDCard("" + System.currentTimeMillis() + ".png", mBmp);
                Toast.makeText(getApplicationContext(), "文件" + fileName + "保存成功！", Toast.LENGTH_SHORT).show();
            }
        });

        // 获取手机截屏
        button2.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {

                mBmp = captureScreen(MainActivity.this);

                System.out.println("bmpWidth=" + mBmp.getWidth());
                System.out.println("bmpHeight=" + mBmp.getHeight());

                mImageView.setBackgroundDrawable(new BitmapDrawable(mBmp));

                Log.i(TAG, "获取截屏");

                String fileName = mFileService.saveBitmapToSDCard("" + System.currentTimeMillis() + ".png", mBmp);
                Toast.makeText(getApplicationContext(), "文件" + fileName + "保存成功！", Toast.LENGTH_SHORT).show();
            }
        });

        // 获取webView可视区域的快照
        button3.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {

                mBmp = captureWebViewVisibleSize(mWebView);

                System.out.println("bmpWidth=" + mBmp.getWidth());
                System.out.println("bmpHeight=" + mBmp.getHeight());

                mImageView.setBackgroundDrawable(new BitmapDrawable(mBmp));

                Log.i(TAG, "获取webView显示区域的截图");

                String fileName = mFileService.saveBitmapToSDCard("" + System.currentTimeMillis() + ".png", mBmp);
                Toast.makeText(getApplicationContext(), "文件" + fileName + "保存成功！", Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 截取webView可视区域的截图
     *
     * @param webView 前提：WebView要设置webView.setDrawingCacheEnabled(true);
     * @return
     */
    private Bitmap captureWebViewVisibleSize(WebView webView) {
        Bitmap bmp = webView.getDrawingCache();
        return bmp;
    }

    /**
     * 截取webView全部内容快照(webView加载的整个内容的大小)
     *
     * @param webView
     * @return
     */
    private Bitmap captureWebView(WebView webView) {
        Picture snapShot = webView.capturePicture();

        Bitmap bmp = Bitmap.createBitmap(snapShot.getWidth(), snapShot.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmp);
        snapShot.draw(canvas);
        return bmp;
    }

    /**
     * 截屏
     *
     * @param context
     * @return
     */
    private Bitmap captureScreen(Activity context) {
        View cv = context.getWindow().getDecorView();
        Bitmap bmp = Bitmap.createBitmap(cv.getWidth(), cv.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmp);
        cv.draw(canvas);
        return bmp;
    }

    /**
     * 回收图片
     */
    public void destoryBitmap() {
        if ((null != mBmp) && (!mBmp.isRecycled())) {
            mBmp.recycle();
            System.out.println("回收图片！");
        }

    }

    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        destoryBitmap();
    }
}
